<?php
defined ( 'BASEPATH' ) or exit ( 'No direct script access allowed' );
class Balancerecord_model extends CI_Model {
	public function __construct() {
		parent::__construct ();
	}
	/*
	 * 记录用户余额变动信息
	 */
	public function create($record){
		$this->db->insert('t_balancerecord', $record);
		return $this->db->insert_id();
	}
	/*
	 * 根据类型和关键字返回充值记录数量
	 */
	public function count_records($type,$name){
		$this->db->join('t_user', 't_balancerecord.userid = t_user.id','left');
		if($type==-2){
			$this->db->where('(t_balancerecord.type=1 or t_balancerecord.type=4)');
		}else{
			$this->db->where('t_balancerecord.type',$type);
		}
		if(!empty($name)){
			$this->db->group_start();
			$this->db->like('t_user.username', $name);
			$this->db->or_like('t_user.shopname', $name);
			$this->db->or_like('t_user.contact', $name);
			$this->db->group_end();
		}
		$query = $this->db->get('t_balancerecord');
		return $query->num_rows();
	}
	/*
	 *  根据类型和关键字返回充值记录
	 */
	public function find_records_by_type($type,$name,$per_page,$offset){//
		$this->db->select('t_balancerecord.id,t_user.shopname,t_balancerecord.num,t_balancerecord.type,t_balancerecord.date');
		$this->db->from('t_balancerecord');
		$this->db->join('t_user', 't_balancerecord.userid = t_user.id','left');
		if($type==-2){
			$this->db->where('(t_balancerecord.type=1 or t_balancerecord.type=4)');
		}else{
			$this->db->where('t_balancerecord.type',$type);
		}
		if(!empty($name)){
			$this->db->group_start();
			$this->db->like('t_user.username', $name);
			$this->db->or_like('t_user.shopname', $name);
			$this->db->or_like('t_user.contact', $name);
			$this->db->group_end();
		}
		$this->db->limit($per_page, $offset);
		$this->db->order_by("date", "desc");
		$query = $this->db->get();
		return $query->result_array();
	}
}